package com.shrio.lucene;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.File;
import java.io.IOException;

/**
 * Created by luojie on 2018/4/24.
 */
public class TestTermQuery {

    public static void main(String[] args) {
        try {
            //1、创建Directory
            Directory directory = FSDirectory.open(new File("G:\\index"));

            //创建索引读取器
            IndexReader reader= DirectoryReader.open(directory);
            //创建索引查询器
            IndexSearcher searcher=new IndexSearcher(reader);
            /**
             * Term直接用一个单词表示，如“hello” ,多个Term用空格分割，如“hello java”
             */
            Query query = new TermQuery(new Term("content","荣耀"));
            TopDocs tds = searcher.search(query, 10);
            System.out.println("一共查询了:"+tds.totalHits);
            for(ScoreDoc sd:tds.scoreDocs) {
                Document doc = searcher.doc(sd.doc);
                System.out.println(doc.get("content"));
            }
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
